package com.logistics.utils.tool;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /** 日期格式 yyyy-mm-dd */
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    /** 日期格式 yyyy/mm/dd */
    public static final String YYYY_MM_DD_SLASH = "yyyy/MM/dd";
    /** 日期格式 yyyy/mm/dd HH:mm:ss */
    public static final String YYYY_MM_DD_HH_MM_DD_SLASH = "yyyy/MM/dd HH:mm:ss";
    /** 日期格式yyyy-mm-dd HH:mm:ss*/
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    /** 日期格式yyyyMMdd */
    public static final String YYYYMMDD = "yyyyMMdd";
    /** 日期格式yyyyMMddHHmmss*/
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    private static final String[] WEEK_DAYS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    /**
     * 把Date转成String
     * @param date
     * @param format
     * @return
     */
    public static String dateToStringFormat(Date date, String format){
        if(StringUtils.isNull(date)){return null;}
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 把String转成Date
     * @param date
     * @param format
     * @return
     */
    public static Date stringToDateFormat(String date, String format){
        if(StringUtils.isNull(date)){return null;}
        Date d = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            d = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    /**
     * 获取本周第一天日期
     * @return
     */
    public static Date getWeekStartDate(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date date = cal.getTime();
        return date;
    }



    /**
     * 获取年的后两位加上月份
     * @return
     */
    public static String getYYmm(){
        String year = dateToStringFormat(getSystemDate(),"yyyy");
        String month = dateToStringFormat(getSystemDate(),"MM");
        return StringUtils.concat("",StringUtils.substring(year,year.length()-2),month);
    }

    /**
     * 获取年的后两位加上月日时
     * @return
     */
    public static String getYYMMddHH(){
        String year = dateToStringFormat(getSystemDate(),"yyyy");
        String month = dateToStringFormat(getSystemDate(),"MM");
        String dd = dateToStringFormat(getSystemDate(),"dd");
        String HH = dateToStringFormat(getSystemDate(),"HH");
        return StringUtils.concat("",StringUtils.substring(year,year.length()-2),month,dd,HH);
    }

    /**
     * 获取年的后两位加上月日时
     * @return
     */
    public static String getYYMMDDHHMMSS(){
        String year = dateToStringFormat(getSystemDate(),"yyyy");
        String month = dateToStringFormat(getSystemDate(),"MM");
        String dd = dateToStringFormat(getSystemDate(),"dd");
        String HH = dateToStringFormat(getSystemDate(),"HH");
        String mm = dateToStringFormat(getSystemDate(),"mm");
        String ss =  dateToStringFormat(getSystemDate(),"ss");
        return StringUtils.concat("",StringUtils.substring(year,year.length()-2),month,dd,HH,mm,ss);
    }

    /**
     * 获取系统时间
     * @return
     */
    public static Date getSystemDate(){
        return new Date();
    }

    /**
     * 获取和系统时间差
     * @param date
     * @return
     */
    public static long getSystemDateDifference(Date date){
        if(StringUtils.isNull(date)){return 0;}
        Date systemDate = getSystemDate();
        long days = (systemDate.getTime() - date.getTime())/(24*3600*1000);
        return days;
    }


    /**
     * 获取当前日期星期
     * @param date
     * @return
     */
    public static String getWeek(Date date){
        if(StringUtils.isNull(date)){return null;}
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return WEEK_DAYS[w];
    }

    /**
     * 根据开始时间获取结束时间
     * @param startDate
     * @param days
     * @return
     */
    public static Date getEndDate(Date startDate, int days){
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        cal.add(cal.DATE, days);
        return cal.getTime();
    }


}
